package com.huishu.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.enums.ResponseEnum;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.order.mapper.ProvinceFreightMapper;
import com.huishu.order.pojo.model.ProvinceFreightDO;
import com.huishu.order.service.ProvinceFreightService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-27
 */
@Service
public class ProvinceFreightServiceImpl extends ServiceImpl<ProvinceFreightMapper, ProvinceFreightDO> implements ProvinceFreightService {

    @Override
    public Result getFreight(String province) {
        ProvinceFreightDO provinceFreight = this.baseMapper.selectOne(Wrappers.lambdaQuery(ProvinceFreightDO.class).like(ProvinceFreightDO::getRegion, province));
        if(provinceFreight == null){
            return Result.success(ResponseEnum.DELIVERY_INFO);
        }
        if(!provinceFreight.getIsDelivery()){
            return Result.success(ResponseEnum.DELIVERY_INFO);
        }
        return Result.success(provinceFreight.getCost());
    }

    @Override
    public Result pageList(BasePage basePage) {
        Page<ProvinceFreightDO> page = new Page<>(basePage.getCurrPage(), basePage.getPageSize());
        LambdaQueryWrapper<ProvinceFreightDO> queryWrapper = Wrappers.lambdaQuery(ProvinceFreightDO.class)
                .eq(StringUtils.isNotBlank(basePage.getSearch()),ProvinceFreightDO::getRegion,basePage.getSearch())
                .orderByAsc(ProvinceFreightDO::getCreatedDate);
        return Result.success(this.baseMapper.selectPage(page,queryWrapper));
    }
}
